
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Runtime.Serialization;
using System.Xml;
using Swaf.Call;
using Swaf.Container;

namespace Swaf.Security
{
	/// <summary>
	/// The DefaultSecurityPrincipal class inherits from SecurityPrincipalBase and 
	/// provides the backward compatible security principal for existing CSF applications.  
	/// It implements the getPrivilege method and overrides the isUserInRole method from 
	/// the SecurityPrincipalBase class.  The getPrivilege method always returns the highest 
	/// privilege, Delete and the isUsesrInRole method always returns true..
	/// </summary>
	[Serializable]
	public class DefaultSecurityPrincipal : SecurityPrincipalBase
	{
		/// <summary>
		/// Constructor for instancing a default security principal from code
		/// </summary>
		/// <param name="UserName">The name of the user associated with this principal</param>
		/// <param name="additionalInfo">INameAddressSupport instance containing application 
		/// specific information about the principal</param>
		/// <param name="rolesCall">ICall instance used to retrieve the roles for a principal.</param>
		public DefaultSecurityPrincipal(string UserName, INameAddressSupport additionalInfo, ICall rolesCall) : base(UserName, additionalInfo, rolesCall)
		{
		}
		
		/// <summary>
		/// Constructor used by the configuration system or to deserialize a serialize principal from an Xml node.
		/// </summary>
		/// <param name="ele">XmlNode containing information about the principal</param>
		public DefaultSecurityPrincipal(XmlElement ele) : base(ele)
		{
			m_additionalInfo = new FlexiMap();
		}
		#region ISecurityPrincipal Members

		/// <summary>
		/// Overridden method to always provide true for any requested role.
		/// <seealso cref="ISecurityPrincipal.isUserInRole"/>
		/// </summary>
		/// <param name="roleName">Requested role</param>
		/// <returns></returns>
		public override bool isUserInRole(string roleName)
		{
			return true;
		}

		/// <summary>
		/// Implementation of abstract method to always return the highest know privilege, Delete for any requested
		/// resource.
		/// <seealso cref="ISecurityPrincipal.getPrivilege"/>
		/// </summary>
		/// <param name="resourceName"></param>
		/// <returns></returns>
		public override Swaf.Security.Privileges getPrivilege(string resourceName)
		{
			return Privileges.Delete;
		}

		public override Swaf.Security.Privileges getPrivilege(string resourceName, bool useDefaultIfNeeded)
		{
			return Privileges.Delete;
		}
		#endregion

		#region INameAddressSupport Members

		public override bool containsField(string nameAddress)
		{
			return true;
		}



		public override object get(string nameAddress)
		{
			if (nameAddress == null || nameAddress.Length==0 )
			{
				return this;
			}
			else
			{
				return Privileges.Delete;
			}
		}

		public override object get(string nameAddress, object oneArg)
		{
			if (nameAddress == null || nameAddress.Length==0 )
			{
				return this;
			}
			else
			{
				return Privileges.Delete;
			}
		}

		public override object get(string nameAddress, params object[] infoArgs)
		{
			if (nameAddress == null || nameAddress.Length==0 )
			{
				return this;
			}
			else
			{
				return Privileges.Delete;
			}
		}


		#endregion
		public DefaultSecurityPrincipal(System.Runtime.Serialization.SerializationInfo info, StreamingContext context) : base(info, context)
		{
		}

	}
}
